package com.zxl.admin.controller.user;

import com.github.pagehelper.PageInfo;
import com.zxl.admin.annotation.NoPower;
import com.zxl.admin.context.UserContext;
import com.zxl.common.Page;
import com.zxl.common.Result;
import com.zxl.common.model.bo.user.UserAddQuery;
import com.zxl.common.model.pojo.SysUser;
import com.zxl.common.model.query.user.UserListQuery;
import com.zxl.common.model.vo.user.UserListVo;
import com.zxl.common.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author zxw
 * @date 2023/3/31
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin/user_manage")
public class UserController {
    @Resource
    private SysUserService sysUserService;

    @ApiOperation("用戶添加")
    @PostMapping("/add")
    public Result add(@RequestBody @Valid UserAddQuery userAdd) {
        sysUserService.doSave(userAdd, userAdd.getRoleId());
        return Result.success("添加成功");
    }

    @ApiOperation("用户列表")
    @GetMapping("/select")
    public Result<Page<UserListVo>> selectUserList(UserListQuery userListQuery){
        List<UserListVo> userListVos = sysUserService.selectUserList(userListQuery);
        Page<UserListVo> page = Page.pageInfoConvert(new PageInfo(userListVos));
        return new Result(page);
    }

    @ApiOperation("删除用户")
    @PostMapping("/delete")
    public Result delete(@RequestParam @ApiParam(value = "用户id", required = true) Integer id){
        sysUserService.delete(id);
        return Result.success();
    }

    @ApiOperation("修改用户")
    @PostMapping("/update")
    public Result update(@RequestBody @Valid UserAddQuery userAdd){
        sysUserService.updateUser(userAdd);
        return Result.success();
    }

    @ApiOperation("个人信息")
    @PostMapping("/info")
    @NoPower
    public Result info() {
        SysUser info = sysUserService.getInfo(UserContext.getUserId());
        return new Result(info);
    }



}
